// pages/food/detail.js
var WxParse = require('../../wxParse/wxParse.js');
import {
    gourmet,
    user,
    marketing,
    wxapp
} from '../../api/apiData'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList: [{
            path: '/images/banner.png',
            url: ''
        }],
        id: '',
        info: '',
        distance_new: '',
        categoryList: [],
        category_id: '',
        list: [],
        list2: [],
        showCoupon:false,
        listCou:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            id: options.id
        })
        this.getDetail()
        this.getCategory()
        gourmet.comment.lists({
            shop_id: this.data.id,
            page: 1,
            size: 5
        }, res => {
            if (res.code == 1) {
                this.setData({
                    list2: res.data.list
                })
            }
        })
        this.getList2()
    },
    getList2() {
        marketing.coupon.lists({
            use_scene:4,
            merchant_id:this.data.id
        }, res => {
            if (res.code == 1) {
                this.setData({
                    listCou: res.data
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    toReceive(e) {
        let id = e.currentTarget.dataset.id
        marketing.coupon.receive({
            coupon_id: id
        }, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg
                })
               setTimeout(()=>{
                   this.getList2()
               },1000)
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    toShowCou(){
        this.setData({
            showCoupon:true
        })
    },
    onCloseCou(){
        this.setData({
            showCoupon:false
        })
    },
    toPre(e) {
        let imgs = e.currentTarget.dataset.imgs
        let index = e.currentTarget.dataset.index
        wx.previewImage({
            urls: imgs,
            current: index
        })
    },
    toPre2(e) {
        let img = e.currentTarget.dataset.img
        let arr = this.data.info.food_list
        let imgs = []
        arr.forEach(item => {
            imgs.push(item.image)
        })
        wx.previewImage({
            urls: imgs,
            current: img
        })
    },
    // 分类
    getCategory() {
        gourmet.category.lists({
            shop_id: this.data.id
        }, res => {
            if (res.code == 1) {
                let arr = [
                    {
                        id:'',
                        name:'全部美食'
                    }
                ]
                this.setData({
                    categoryList: arr.concat(res.data),
                })
                this.getList()
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    onChange(e) {
        console.log(e)
        this.setData({
            category_id: e.detail.name
        })
        this.getList()
    },
    getList() {
        gourmet.product.lists({
            category_id: this.data.category_id,
            shop_id: this.data.id
        }, res => {
            if (res.code == 1) {
                this.setData({
                    list: res.data
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    // 收藏
    toCollect() {
        user.collect.submit({
            module: 'gourmet_shop',
            module_id: this.data.info.id
        }, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.data.message,
                })
                let has_collect = this.data.has_collect == 0 ? 1 : 0
                this.setData({
                    has_collect
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    getDetail() {
        let that = this
        gourmet.shop.detail({
            shop_id: this.data.id
        }, res => {
            if (res.code == 1) {
                this.setData({
                    info: res.data,
                    has_collect: res.data.has_collect
                })
                WxParse.wxParse('bindName', 'html', res.data.intro_content, this, 5)
                wx.getLocation({
                    type: 'wgs84',
                    success: function (re) {
                        let distance_new = that.distance(re.latitude, re.longitude, res.data.latitude, res.data.longitude)
                        that.setData({
                            distance_new: distance_new
                        })
                    }
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    toMap() {
        let infos = this.data.info
        let latitude = Number(infos.latitude)
        let longitude = Number(infos.longitude)
        wx.getLocation({
            type: 'wgs84',
            success: function (res) {
                wx.openLocation({ //​使用微信内置地图查看位置。
                    latitude: latitude, //要去的纬度-地址
                    longitude: longitude, //要去的经度-地址
                    name: infos.name,
                    address: infos.address
                })
            },
            fail(res) {
                console.log(res)
            }
        })
    },
    toCall() {
        let infos = this.data.info
        wx.makePhoneCall({
            phoneNumber: infos.telephone,
        })
    },
    toUrl(e) {
        let url = e.currentTarget.dataset.url
        wx.navigateTo({
            url: url,
        })
    },
    distance(la1, lo1, la2, lo2) {
        var La1 = (la1 * Math.PI) / 180.0
        var La2 = (la2 * Math.PI) / 180.0
        var La3 = La1 - La2
        var Lb3 = (lo1 * Math.PI) / 180.0 - (lo2 * Math.PI) / 180.0
        var s =
            2 *
            Math.asin(
                Math.sqrt(
                    Math.pow(Math.sin(La3 / 2), 2) +
                    Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)
                )
            )
        s = s * 6378.137
        s = Math.round(s * 10000) / 10000
        s = s.toFixed(2)
        return s
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getInfo()
    },
    getInfo() {
        let token = wx.getStorageSync('token')
        if (token) {
            // 获取用户详情
            user.detailS({}, res => {
                if(res.code==20003){
					this.getUserInfos()
                }else{
                   
                }
                
            })
        } else {
			this.getUserInfos()

        }
    },
 	/**
	 * 授权登录
	 */
	getUserInfos(e) {
        let that = this
		wx.login({
			success(res) {
				console.log(res)
				let code = res.code
				wx.getUserInfo({
					success: (res) => {
						var encryptedData = res.encryptedData;
						var iv = res.iv
						var info = res.rawData
						var obj = {
							code: code,
							iv: iv,
							encryptedData: encryptedData
						}
						wxapp.login(obj, res => {
							if (res.code == 1) {
								wx.setStorage({
									key: "token",
									data: res.data.token
                                })
                                that.getDetail()
							} else {
								wx.showToast({
									title: res.msg,
									icon: 'none'
								})
							}
						})
					},
					fail: res => {

					}
				})
			}
		})
    },
   
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})